package com.daixu.xutil.demo.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashSet;
import java.util.Set;

public class PreferenceUtils {

	private static SharedPreferences preferences;
	
	public static final boolean DEF_BOOLEAN = false;
	public static final int DEF_INT = 0;
	public static final float DEF_FLOAT = 0.0f;
	public static final long DEF_LONG = 0L;
	public static final String DEF_STRING = "";

	public static void init(Context context) {
		if (preferences == null) {
			synchronized (PreferenceUtils.class) {
				preferences = context.getSharedPreferences(
						context.getPackageName(), Context.MODE_PRIVATE);
			}
		}
	}

	@SuppressWarnings("unchecked")
	public static void put(String key, Object obj) {
		if (obj instanceof Boolean)
			put(key, ((Boolean) obj).booleanValue());
		else if (obj instanceof Float)
			put(key, ((Float) obj).floatValue());
		else if (obj instanceof Integer)
			put(key, ((Integer) obj).intValue());
		else if (obj instanceof Long)
			put(key, ((Long) obj).longValue());
		else if (obj instanceof String)
			put(key, obj.toString());
		else if (obj instanceof Set && DeviceUtils.getSDKInt() >= 11)
			put(key, (Set<String>)obj);
	}
	
	public static void put(String key, boolean value){
		preferences.edit().putBoolean(key, value).commit();
	}
	
	public static void put(String key, float value){
		preferences.edit().putFloat(key, value).commit();
	}
	
	public static void put(String key, long value){
		preferences.edit().putLong(key, value).commit();
	}
	
	public static void put(String key, int value){
		preferences.edit().putInt(key, value).commit();
	}
	
	public static void put(String key, String value){
		preferences.edit().putString(key, value).commit();
	}
	
	@SuppressLint("NewApi")
	public static void put(String key, Set<String> value){
		preferences.edit().putStringSet(key, value).commit();
	}
	
	/*******************************************************
	 * 说明：返回默认为false的preference值
	 * @param key
	 * @return
	 *******************************************************
	 */
	public static boolean getBoolean(String key){
		return getBoolean(key, DEF_BOOLEAN);
	}
	
	public static boolean getBoolean(String key, boolean defValue){
		return preferences.getBoolean(key, defValue);
	}
	
	/*******************************************************
	 * 说明：返回默认为0.0f的preference值
	 * @param key
	 * @return
	 *******************************************************
	 */
	public static float getFloat(String key){
		return getFloat(key, DEF_FLOAT);
	}
	
	public static float getFloat(String key, float defValue){
		return preferences.getFloat(key, defValue);
	}
	
	/*******************************************************
	 * 说明：返回默认为0L的preference值
	 * @param key
	 * @return
	 *******************************************************
	 */
	public static long getLong(String key){
		return getLong(key, DEF_LONG);
	}
	
	public static long getLong(String key, long defValue){
		return preferences.getLong(key, defValue);
	}
	
	/*******************************************************
	 * 说明：返回默认为0的preference值
	 * @param key
	 * @return
	 *******************************************************
	 */
	public static int getInt(String key){
		return getInt(key, DEF_INT);
	}
	
	public static int getInt(String key, int defValue){
		return preferences.getInt(key, defValue);
	}
	
	/*******************************************************
	 * 说明：返回默认为""的preference值
	 * @param key
	 * @return
	 *******************************************************
	 */
	public static String getString(String key){
		return getString(key, DEF_STRING);
	}
	
	public static String getString(String key, String defValue){
		return preferences.getString(key, defValue);
	}
	
	/*******************************************************
	 * 说明：返回默认为EmptySet的preference值 <br />
	 * 需要API版本为11以上
	 * @param key
	 * @return
	 *******************************************************
	 */
	public static Set<String> getStringSet(String key){
		return getStringSet(key, new HashSet<String>());
	}
	
	@SuppressLint("NewApi")
	public static Set<String> getStringSet(String key, Set<String> defValue){
		if(DeviceUtils.getSDKInt() >= 11)
			return preferences.getStringSet(key, defValue);
		else
			return null;
	}
	
	public static boolean contains(String key){
		return preferences.contains(key);
	}
	
	public static boolean remove(String key){
		if(contains(key))
			return preferences.edit().remove(key).commit();
		return false;
	}
	public static void clear(){
		preferences.edit().clear().commit();
	}
}